﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AmazonGlacierGUI.UI.Infrastructure.MVVMFramework.Controls
{
    /// <summary>
    /// Interaction logic for ValidationSummary.xaml
    /// </summary>
    public partial class ValidationSummary : UserControl
    {
        public ValidationSummary()
        {
            InitializeComponent();
            this.DataContextChanged += new DependencyPropertyChangedEventHandler(ValidationSummary_DataContextChanged);
        }

        void ValidationSummary_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if(e.NewValue==null||(e.NewValue is AppViewModelBase)==false)
            {
                this.Visibility = Visibility.Collapsed;
                return;                
            }
            var vm = (AppViewModelBase) e.NewValue;
            vm.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(vm_PropertyChanged);
            SetUI(vm);
        }

        void vm_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName != "HasErrors") return;          
            SetUI((AppViewModelBase) sender);

        }
        void SetUI(AppViewModelBase vm)
        {
            if (vm.HasErrors == false)
            {
                this.Visibility = Visibility.Collapsed;
                return;
            }
            this.Visibility = Visibility.Visible;
            tbError.Text = String.Format("({0}) Error", vm.GetAllErrors().Count);
            tbError.ToolTip = string.Join("\n", vm.GetAllErrors().Select(p => p.ErrorMessage).ToArray());
        }

     
    }
}
